const path = require("path");
const fs = require("fs");

// 📄 读取 result_stream.jsonl 文件
const inputPath = path.join(__dirname, "result_stream_v2.jsonl");
const lines = fs.readFileSync(inputPath, "utf8").split("\n").filter(Boolean);

let totalCount = 0;
let accurateCount = 0;
let currentConsecutiveErrors = 0;
let maxConsecutiveErrors = 0;
let errorStreaks = []; // 用于记录连续错误段的长度

for (const line of lines) {
  const entry = JSON.parse(line);
  const isAccurate = entry.isAccurate;

  totalCount++;

  if (isAccurate === 1 || isAccurate === true) {
    accurateCount++;
    if (currentConsecutiveErrors > 0) {
      errorStreaks.push(currentConsecutiveErrors);
      if (currentConsecutiveErrors > maxConsecutiveErrors) {
        maxConsecutiveErrors = currentConsecutiveErrors;
      }
      currentConsecutiveErrors = 0;
    }
  } else {
    currentConsecutiveErrors++;
  }
}

// 收尾处理
if (currentConsecutiveErrors > 0) {
  errorStreaks.push(currentConsecutiveErrors);
  if (currentConsecutiveErrors > maxConsecutiveErrors) {
    maxConsecutiveErrors = currentConsecutiveErrors;
  }
}

// 📊 输出分析统计
console.log(`\n📊 准确率统计:`);
console.log(`总期数: ${totalCount}`);
console.log(`命中次数: ${accurateCount}`);
console.log(`未命中次数: ${totalCount - accurateCount}`);
console.log(`准确率: ${(accurateCount / totalCount * 100).toFixed(2)}%`);

console.log(`\n📉 错误分析:`);
console.log(`最大连续错误: ${maxConsecutiveErrors}`);
console.log(`错误段数: ${errorStreaks.length}`);
console.log(`平均连续错误: ${
  errorStreaks.length
    ? (errorStreaks.reduce((a, b) => a + b, 0) / errorStreaks.length).toFixed(2)
    : 0
}`);
console.log(`当前连续错误: ${currentConsecutiveErrors}`);